package com.jmpt.fangbohui.ui.personalcenter;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.google.gson.Gson;
import com.jmpt.fangbohui.Constants;
import com.jmpt.fangbohui.R;
import com.jmpt.fangbohui.adapter.SeeHouseClientAdapter;
import com.jmpt.fangbohui.bean.SeeHouseClientBean;
import com.jmpt.fangbohui.bean.SeeHouseClientBean.date_value;
import com.jmpt.fangbohui.dao.LoginSQLiteOperator;
import com.jmpt.fangbohui.ui.base.BaseHeadActivity;
import com.jmpt.fangbohui.utils.DebugLog;
import com.jmpt.fangbohui.utils.ToastHelper;

public class SeeHouseClientActivity extends BaseHeadActivity {

	public final static int SUCCESS = 200;
	public final static int FAILED = 202;

	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch(msg.what){
			case SUCCESS:
				String str = (String) msg.obj;
				parser2(str);
				hideLoading();
			break;
			case FAILED:
				String str2 = (String) msg.obj;
				ToastHelper.ShowToast(str2, mContext);
				hideLoading();
			break;
			}
			
		};
	};
	private ListView lv_see_house_client;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_see_house_client);
		init();
		initView();
		
	}

	protected void parser2(String str) {

		DebugLog.i(str);
		Gson gson = new Gson();
		SeeHouseClientBean bean = gson.fromJson(str.trim(), SeeHouseClientBean.class);
		if(bean.done.equals("true")){
			List<date_value> date_value = bean.retval.date_value;
			SeeHouseClientAdapter mAdapter = new SeeHouseClientAdapter(this, date_value);
			lv_see_house_client.setAdapter(mAdapter);
		}else{
			ToastHelper.ShowToast(bean.msg, mContext);
		}		
	}

	private void initView() {
		showTitle("预约看房客户");
		showBackButton(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		lv_see_house_client = (ListView) findViewById(R.id.lv_see_house_client);
	}

	private void init() {
		LoginSQLiteOperator oper = new LoginSQLiteOperator(this);
		final List<String> querySessionId = oper.querySessionId();
		if(querySessionId.size() != 2){
			return;
		}
		showLoading();
		
		
		new Thread(){
			public void run() {
				HttpClient httpClient = new DefaultHttpClient();
				try {
					List<NameValuePair> params = new ArrayList<NameValuePair>();
					params.add(new BasicNameValuePair("AgentId",querySessionId.get(0)));
					
				    HttpPost postMethod = new HttpPost(Constants.URL.SEEHOUSECLIENT);
				    postMethod.setHeader("Cookie", "PHPSESSID=" + querySessionId.get(1)); 
				    postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
								
				    HttpResponse response = httpClient.execute(postMethod); //执行POST方法
				    DebugLog.i("resCode = " + response.getStatusLine().getStatusCode());
				    
				    if(response.getStatusLine().getStatusCode() != 200){
				    	Message msg = Message.obtain();
					    msg.what = FAILED;
					    msg.obj = "网络暂不畅通，请稍后再试";
					    handler.sendMessage(msg);
				    	return;
				    }
				    parser(EntityUtils.toString(response.getEntity(), "utf-8"));
				    
								
				} catch (Exception e) {
				    e.printStackTrace();
				} 
			};
		}.start();
	}

	protected void parser(String string) {
		Message msg = Message.obtain();
	    msg.what = SUCCESS;
	    msg.obj = string;
	    handler.sendMessage(msg);
	}
}
